package com.weipai.springboot.module.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.weipai.springboot.enums.RetCode;
import com.weipai.springboot.model.Brand;
import com.weipai.springboot.model.Demand;
import com.weipai.springboot.model.api.Response;
import com.weipai.springboot.module.service.DemandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 曹朋飞
 * @Date: 2018/08/06 18:00
 * <p>
 * 需求管理类
 */
@RestController
public class DemandController {

    @Autowired
    DemandService demandService;

    /**
     * 分页查询
     *
     * @param page             页码
     * @param size             每页条数
     * @param draw             前台参数，照样返回chang
     * @param demandCategoryId 需求的类别id
     */
    @GetMapping("/demand/list/{page}/{size}/{draw}/{demandCategoryId}")
    public Response list(@PathVariable("page") Integer page,
                         @PathVariable("size") Integer size,
                         @PathVariable("draw") String draw,
                         @PathVariable("demandCategoryId") String demandCategoryId) {
        Response response = new Response();
        PageHelper.startPage(page, size);
        List<Demand> list = demandService.selectAllByDemand(demandCategoryId);
        PageInfo<Demand> pageInfo = new PageInfo<>(list);
        if (list.size() > 0) {
            response.setCode(RetCode.SUCCESS);
            response.put("pageData", pageInfo.getList());
            response.put("recordsTotal", pageInfo.getTotal());
            response.put("draw", draw);
            response.put("pages", pageInfo.getPages());
        } else {
            response.setCode(RetCode.NO_DATA);
        }
        return response;
    }

    /**
     * 根据需求状态分页查询
     *
     * @param page         页码
     * @param size         每页条数
     * @param draw         前台参数，照样返回chang
     * @param demandStatus 需求状态:0 未审核， 4  已审核 待处理 ,  1客服介入，2已完成，3已驳回,
     */
    @GetMapping("/demand/listByStatus/{page}/{size}/{draw}/{demandStatus}")
    public Response list(@PathVariable("page") Integer page,
                         @PathVariable("size") Integer size,
                         @PathVariable("draw") String draw,
                         @PathVariable("demandStatus") Integer demandStatus) {
        return demandService.selectDemandByStatus(page, size, draw, demandStatus);
    }

    /**

     ** 更改需求的状态--0 未审核， 4  已审核 待处理 ,  1客服介入，2已完成，3已驳回,
     * @param id:需求id
     * @param status:status
     * @return
     */
    @GetMapping("/demand/changeStatus/{id}/{status}")
    public Response changeStatus(@PathVariable("id") String id,
                                 @PathVariable("status") Integer status) {
        return demandService.changeStatus(id, status);
    }
   /* @RequestMapping(value = "/demand/changeStatus", method = RequestMethod.GET)
    public Response changeStatus(@RequestParam(value = "id") String id,
                                 @RequestParam(value = "status") Integer status){
        return null;
    }*/

    /**
     * 查询需求详情
     *
     * @param id:需求的id
     * @return
     */
    @GetMapping("/demand/getDemandById/{id}")
    public Response getDemandById(@PathVariable("id") String id) {
        return demandService.getDemandById(id);
    }

    /**
     * 首页需求 根据城市查需求       APp
     * @param status：状态值
     * @param page：页码
     * @param size：每页条数
     * @return
     */
    @GetMapping("/demand/demandListByCity/{status}/{page}/{size}")
    public Response demandListByCity(  @PathVariable("status") Integer status,
                                     @PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        return demandService.demandListByCity( status, page, size);
    }

    /**
     *  根据用户查需求               --------------------------发布人查需求
     * @param customerId: 发布人  用户id
     * @param status：状态值
     * @param page：页码
     * @param size：每页条数
     * @return
     */
    @GetMapping("/demand/demandListByCustomer/{customerId}/{status}/{page}/{size}")
    public Response demandListByCustomer(@PathVariable("customerId") String customerId, @PathVariable("status") Integer status,
                                         @PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        return demandService.demandListByCustomer(customerId, status, page, size);
    }



    /**
     *  根据用户查需求               --------------------------技工查需求
     * @param artisanId: 技工  id
     * @param status：状态值
     * @param page：页码
     * @param size：每页条数
     * @return
     */
    @GetMapping("/demand/demandListByArtisanId/{artisanId}/{status}/{page}/{size}")
    public Response demandListByArtisanId(@PathVariable("artisanId") String artisanId,
                                          @PathVariable("status") Integer status,
                                          @PathVariable("page") Integer page,
                                          @PathVariable("size") Integer size) {
        return demandService.demandListByArtisanId(artisanId, status, page, size);
    }



    /**
     * 将待处理改为处理中状态,保存客服姓名和工号
     *
     * @param name      //客服名称
     * @param loginName 工号(登录名)
     * @return
     */
    @PostMapping("/demand/handlePending")
    public Response handlePending(String demandId, String name, String loginName) {
        return demandService.handlePending(demandId, name, loginName);
    }

    /**
     * 保存技工信息,不修改状态
     *
     * @param demandId
     * @param name
     * @param mobile
     * @param amount   金额,单位:分
     * @return
     */
    @PostMapping("/demand/saveArtisanInfo")
    public Response saveArtisanInfo(String demandId, String name, String mobile, int amount) {
        return demandService.saveArtisanInfo(demandId, name, mobile, amount);
    }

    /**
     * 完成订单
     *
     * @param demandId
     * @return
     */
    @PostMapping("/demand/complete")
    public Response complete(String demandId) {
        return demandService.complete(demandId);
    }


    /**
     * 更改需求信息
     *
     * @param demand
     * @return
     */
    @PostMapping("/demand/update")
    public Response update(Demand demand) {
        return demandService.update(demand);
    }
}
